package com.bw.sparksql1.job1

import org.apache.log4j.{Level, Logger}
import org.apache.spark.rdd.RDD
import org.apache.spark.sql.types.{IntegerType, StringType, StructField, StructType}
import org.apache.spark.sql.{DataFrame, Row, SparkSession}

/**
  *
  *  RDD -> DataFrame 方式2
  *
  */
object Job7 {
    def main(args: Array[String]): Unit = {
      Logger.getLogger("org").setLevel(Level.ERROR)
      val spark = SparkSession
        .builder()
        .master("local")
        .appName("Spark SQL basic example")
        .getOrCreate()

      val rdd1 = spark.sparkContext.textFile("person4.txt")
      import spark.implicits._
      val rowRDD: RDD[Row] = rdd1.map(line => {
        val fields = line.split(",")
        Row(fields(0), fields(1).trim.toInt)
      })

      val structType = StructType(
        StructField("name", StringType, true) ::
        StructField("age", IntegerType, true) :: Nil
      )

      val df: DataFrame = spark.createDataFrame(rowRDD,structType)
      df.createOrReplaceTempView("people")
      spark.sql("select name from people").show()
    }
}
